package csu.jpetstore.jpetstore.service;

import csu.jpetstore.jpetstore.domain.Category;
import csu.jpetstore.jpetstore.domain.Product;
import csu.jpetstore.jpetstore.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    @Autowired
    ProductRepository productRepository;

    @Autowired
    CategoryService categoryService;

    public Product getProduct(int id){
        return productRepository.findById(id).orElse(null);
    }


    public List<Product> getAllByCategoryId(int id){
        Category category = categoryService.getCategory(id);
        return productRepository.findAllByCategory(category);
    }

    public List<Product> getAllByNameLike(String keyword){
        return productRepository.findAllByNameLike("%"+keyword+"%");
    }
}
